package chapter06

/**
 * Created with IntelliJ IDEA.
 * ClassName: Test14_TraitMixin
 * Package: chapter06
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 *          Date: 2023-06-23
 *          Time: 19:02
 */

object Test14_TraitMixin {
  def main(args: Array[String]): Unit = {
    //特征的混入
    val stu = new Student14
    stu.study()
    stu.increase()

    stu.play()
    stu.increase()

    stu.dating()
    stu.increase()

    //动态混入 不是写好的混入 十二动态的混入
    //意思就是创建有天赋的学生在把这个体制混入进去 new的时候混入特质
    val student = new Student14 with Talent{
      override def dancing(): Unit = println("student is dancing")
      override def singing(): Unit = println("student is singing")
      //也可以是由普通的
    }
    student.play()
    student.dating()
    student.dancing()
  }
}

//学生的天赋 不一定所有的学生都用天赋
trait Talent{
  def singing():Unit
  def dancing():Unit
}

//在定义一个特质
trait Knowledge {
  var amount: Int = 0
  def increase(): Unit
}
//定义一个子类
class Student14 extends Person13 with Yong with Knowledge {
  def increase(): Unit = {
    amount += 1
    println(s"student ${name} Knowledge increased: ${amount}")
  }

  //父类当中有name 特质当中也有name 继承了父类 实现了特质 相同的属性就冲突了
  //编译器没有报错 但是运行错误 解决办法是 在子类中 重新申明冲突的属性
  override val name: String = "student"
  //同理  父类有age 特质有抽象的age 所以补充些也没问题
  def dating(): Unit = println(s"student ${name} is dating ${age} 555")
  def study() = println(s"student $name is studying")

  //重写父类方法
  override def sayHello(): Unit = {

    super.sayHello()
    println(s"hello from: student $name")
  }

}